﻿using Furion;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using XinGuanERP.Attributes.system;
using XinGuanERP.Models.Po.system;
using XinGuanERP.Models.Vo.system;
using XinGuanERP.Services.system.IServices;
using XinGuanERP.Utils;

namespace XinGuanERP.Controllers.system
{
    [DynamicApiController]
    public class UserLoginController
    {
        private readonly IUserLoginService _userLoginService;
        private readonly ISysMenuService _sysMenuService;
        public UserLoginController(IUserLoginService userLoginService, ISysMenuService sysMenuService)
        {
            _userLoginService = userLoginService;
            _sysMenuService = sysMenuService;
        }

        [ServiceFilter(typeof(MyActionFilterAttribute))]
        [HttpPost]
        public ResultUtil Login(UserLoginVo userLoginVo) =>
            ResultUtil.ok(_userLoginService.Login(userLoginVo));

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil GetUserInfo()
        {
            var us = new UserVo()
            {
                UserId = App.User?.FindFirstValue("UserId"),
                username = App.User?.FindFirstValue("UserName"),
                RealName = App.User?.FindFirstValue("RealName"),
                Avatar = App.User?.FindFirstValue("Avatar"),
                RoleId = App.User?.FindFirstValue("RoleId"),
                HomePath = App.User?.FindFirstValue("HomePath")
            };
            return ResultUtil.ok(us);
        }

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        public ResultUtil GetPermCodeWithList()
            => ResultUtil.ok(_sysMenuService.GetGrantPermissions());

        [Authorize]
        [ServiceFilter(typeof(MyActionFilterAttribute))]
        [HttpGet]
        public ResultUtil Logout()
        {
            return ResultUtil.ok(null);
        }
    }
}
